/**
 * org.vz.product.collect.maintain.service.impl
 * @since : jdk 1.8
 */
package org.vz.product.collect.maintain.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vz.product.collect.maintain.dao.MessageChannelDao;
import org.vz.product.collect.maintain.domain.MessageChannelEntity;
import org.vz.product.collect.maintain.domain.MessageResult;
import org.vz.product.collect.maintain.domain.entity.MessageChannelResult;
import org.vz.product.collect.maintain.service.ActiveMqService;
import org.vz.product.collect.maintain.service.MessageChannelService;

/**
 * @author Morik is Yongxu.Wang 消息通道
 * @CreateTime 2017年10月13日 15:46:46
 * @version 1.0.0
 * @description
 */
@Service
public class MessageChannelServiceImpl implements MessageChannelService {
	@Autowired
	MessageChannelDao messageChannelDao;
	@Autowired
	ActiveMqService amService;

	@Override
	public MessageChannelResult pageList(String startTime, String endTime) {

		String baseUrl = "http://172.16.6.21:8161/api/jolokia";
		// 查询相应通道的未出队消息数
		List<MessageResult> totalMessageCount = amService.totalMessageCount(
				baseUrl, startTime, endTime);

		MessageChannelResult mcrs = new MessageChannelResult();
		Map<String, String> prmgr = new HashMap<String, String>();
		prmgr.put("startTime", startTime);
		prmgr.put("endTime", endTime);
//		query.setParas(prmgr);
		// 分页查询消息通道详情
		List<MessageChannelEntity> pageQuery = messageChannelDao
				.getSQLManager().select("messagelog.getMsage",
						MessageChannelEntity.class, prmgr);

		if (pageQuery != null) {
			mcrs.data.data = pageQuery;
			mcrs.msg = "查询成功!!!";
			mcrs.status = 1;
//			mcr.data.pageCounts = pageQuery.getTotalPage();
//			mcr.data.pageNumber = pageQuery.getPageNumber();
		}
		if (totalMessageCount.size() > 0 && pageQuery.size() > 0) {
			for (MessageChannelEntity iterable : mcrs.data.data) {
				for (MessageResult s : totalMessageCount) {
					if (iterable.getReceiveEventNameCN().equals(s.msgSendingName)) {
						// 带出队数
						iterable.setMsgSendingNum(s.msgacceptNum);
						// 入队数
						iterable.setMsgacceptNum(s.msgacceptNum + iterable.getMsgHealthyRate());
						// 消息通道名
//						iterable.setMsgSendingName(s.receiveEventNameCN);
						mcrs.outNum += s.msgacceptNum;
						break;
					}
				}
			}
		}
		// 总数统计
		List<MessageChannelEntity> count = messageChannelDao.getSQLManager()
				.select("messagelog.getAllCount", MessageChannelEntity.class,
						prmgr);
		// 广播总数               带出队总数
		for (MessageChannelEntity iterable : count) {
			mcrs.atuNum += iterable.getMsgHealthyRate();
			
		}
		

		mcrs.inNum = mcrs.atuNum + mcrs.outNum;
		return mcrs;
	}

//	@Override
//	public MessageChannelResult pageList(String startTime, String endTime) {
//		MessageChannelResult mcr = new MessageChannelResult();
//		Map<String, String> prmgr = new HashMap<String, String>();
//		prmgr.put("startTime", startTime);
//		prmgr.put("endTime", endTime);
//
//		List<MessageChannelEntity> datas = messageChannelDao
//				.getSQLManager()
//				.select("messagelog.getMsgs", MessageChannelEntity.class, prmgr);
//
//		if (datas != null) {
//			mcr.msg = "查询成功!!!";
//			mcr.status = 1;
//			mcr.data.data = datas;
//		}
//		return mcr;
//	}

}
